package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guguskins.back.entity.GuStock;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/stock")
public class GuStockController {

    @Autowired
    private GuStockService stockService;

    @GetMapping("/back/page")
    public ResultVo backPage(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                             @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                             @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        Page<GuStock> stockPage = stockService.page(new Page<>(p, size), new LambdaQueryWrapper<GuStock>() {
            {
                like(!ObjectUtils.isEmpty(searchKey), GuStock::getStockAccountNumber, searchKey);
                or();
                like(!ObjectUtils.isEmpty(searchKey), GuStock::getStockMarketName, searchKey);
            }
        });
        return ResultVo.success(new PageVo<>(stockPage.getRecords(), stockPage.getTotal()));
    }

    @PostMapping("/back/update")
    public ResultVo backUpdate() {
        if (stockService.backUpdate()) {
            return ResultVo.success("更新成功");
        }
        return ResultVo.fail("更新失败");
    }
}
